<?php
include_once "lib/config.php";
include_once "header.php";


if(isset($_POST['save'])&& $_POST['save']!="")
{
	if(isset($user_details[0]['profile_photo']) && $user_details[0]['profile_photo']!=Null){
		 if(file_exists($user_details[0]['profile_photo']))
		 {
			$new_path=	$user_details[0]['profile_photo'];
		 }  
		 else
		 {
			$new_path="";
		 }
	}
	if($_FILES['profile_photo']['name']!="")
    {
        include_once "thumb.php";
        if($_FILES['profile_photo']['type']=="image/jpeg" || $_FILES['profile_photo']['type']=="image/gif" || $_FILES['profile_photo']['type']=="image/png" || $_FILES['profile_photo']['type']=="image/jpg" || $_FILES['profile_photo']['type']=="image/JPEG" || $_FILES['profile_photo']['type']=="image/GIF" || $_FILES['profile_photo']['type']=="image/PNG" || $_FILES['profile_photo']['type']=="image/JPG")
        {
            list($width, $height) = getimagesize($_FILES['profile_photo']["tmp_name"]);
            $folder="uloaded_files";
            $folder_val="uloaded_files/profile_photo";
            $exact_folder_val="uloaded_files/profile_photo/".$_SESSION['u_id'];
            $extension_arr=explode("/",$_FILES['profile_photo']['type']);
            $extension=$extension_arr[1];
            
            $f=$_FILES['profile_photo']['name'];
            $path=$folder_val."/".$f;
            
            //$tem=$_FILES['profile_photo']['tmp_name'];
            
            
                        if(file_exists($folder) && is_dir($folder))
                        {
                            if(file_exists($folder_val) && is_dir($folder_val))
                            {
                                if(file_exists($exact_folder_val) && is_dir($exact_folder_val))
                                {
                                    
                                }
                                else
                                {
                                    if(mkdir($exact_folder_val,0777))
                                    {
                                        //echo " ->  Directory Created..";
                                    }
                                }
                            }
                            else
                            {
                                if(mkdir($folder_val,0777))
                                {
                                    //echo " ->  Directory Created..";
                                }
                                if(mkdir($exact_folder_val,0777))
                                {
                                    //echo " ->  Directory Created..";
                                }
                            }
                                
                        }
                        else
                        {
                            if(mkdir($folder,0777))
                            {
                                //echo "--> outer folder created ..";
                            }
                            if(mkdir($folder_val,0777))
                            {
                                //echo " ->  Directory Created..";
                            }
                            if(mkdir($exact_folder_val,0777))
                            {
                                //echo " ->  Directory Created..";
                            }
                            
                        }
                    $target=$folder_val."/";
                    
                    $new_path=$exact_folder_val."/".$_FILES['profile_photo']['name'];
                    
                    if($width>140 || $height>196)
                    {
                        $move= move_uploaded_file($_FILES['profile_photo']['tmp_name'], $new_path);
                        if($move)
                        {
                            createThumbsforProfiles_widthTransparent($new_path,$new_path,140,196,"width");
                        }
                        
                    }
                    else
                    {
                        $move= move_uploaded_file($_FILES['profile_photo']['tmp_name'], $new_path);
                    }
                
        }    
    }
    
    $_POST['User']['profile_photo']=$new_path;
    $_POST['User']['dob']=convertDateToTime($_POST['User']['dob']);
    DB::updateData("users",$_POST['User'],array("id"=>$_SESSION['u_id']));
    
    if(DB::$affectedrows>0)
	{
		$_SESSION['msg_success']="Success: Profile updated successfully";
		
	}
	else
	{
		$_SESSION['msg_error']="Error: Profile not updated";
	}
	header("Location:dashboard.php");
	exit();
}

if(isset($user_details[0]['dob']) && $user_details[0]['dob']!=Null)
	$dob= $user_details[0]['dob'];
else
	$dob="";	


?>
  <link rel="stylesheet" href="css/jquery-ui.css" />
  <script src="js/jquery-1.9.1.js"></script>
  <script src="js/jquery-ui.js"></script>		
  <script>
  $(function() {
    $('#dob').datepicker({
		dateFormat: 'dd/mm/yy',
		changeMonth: true,
	    changeYear: true,
	    yearRange: '1950:<?php echo date("Y"); ?>',
	    
	    inline: true
		}).datepicker('setDate', '<?php echo $dob; ?>');
    
  });
  
  </script>     
		<div class="container_12" id="main_div">
           <form action="" method="post" onsubmit="return valideditagent_profile();" enctype="multipart/form-data">
			<div class="float-left" style="padding-left:20px">
		        <a href="dashboard.php" class="button">
		        	<span>View Dashboard</span>
		        </a>
		    </div><br><br>
			<div class="grid_12" >
		    	
		        <div class="module">
		        		
		             <h2><span  onclick="toggle_div('basic_body','basictoogleimg');"><img src="images/desc.gif" height="5px" id="basictoogleimg">&nbsp;&nbsp;Basic Information</span></h2>
		                
		             <div class="module-body" id="basic_body">
		                
		                	<?php
		                	if(isset($msg_success) && $msg_success!="")
		                	{
		                	?>
		                    <div>
		                        <span class="notification n-success"><?php echo $msg_success; ?></span>
		                    </div>
		                    <?php
		                    }
		                    if(isset($msg_error) && $msg_error!="")
			                {
			                ?>
			                <span class="notification n-error"><?php echo $msg_error; ?></span>
			                <?php
							}
			                ?>
		                    
		                    <p>
		                        <label>Full Name<span style="color: red;">*</span></label>
		                        <input type="text" class="input-short" style="width: 280px;" id="name" name="User[full_name]" value="<?php if(isset($user_details[0]['full_name']) && $user_details[0]['full_name']!=Null)echo $user_details[0]['full_name']; ?>"/>
		                        
		                    </p>
		                    
		                    <p>
		                        <label>Gender<span style="color: red;">*</span></label>
		                        <select class="input-short" id="gender" name="User[gender]" style="width: 280px;">
		                            <option value="">Select Gender</option>
		                            <option value="Male" <?php if(isset($user_details[0]['gender']) && $user_details[0]['gender']!=Null){ if($user_details[0]['gender']=="Male")echo "selected"; } ?>>Male</option>
		                            <option value="Female" <?php if(isset($user_details[0]['gender']) && $user_details[0]['gender']!=Null){ if($user_details[0]['gender']=="Female")echo "selected"; } ?>>Female</option>
		                        </select>
		                    </p>
		                    
		                    
		                    <p>
		                        <label>Date of Birth<span style="color: red;">*</span></label>
		                        <input type="text" name="User[dob]" value="" id="dob" readonly="true" class="input-short" style="width: 280px;">
		                    </p>
		                    
		                    <p>
		                        <label>Profile Picture</label>
		                        <input type="file" name="profile_photo" id="profile_photo" class="input-short" style="width: 280px;">
		                    </p>
		                    <p>
		                        <label>Email<span style="color: red;">*</span></label>
		                        <input type="text" name="User[email_id]" id="email_id" class="input-short" style="width: 280px;" value="<?php if(isset($user_details[0]['email_id']) && $user_details[0]['email_id']!=Null)echo $user_details[0]['email_id']; ?>">
		                    </p>
		                    <p>
		                        <label>Contact Number<span style="color: red;">*</span></label>
		                        <input type="text" name="User[contact_number]" id="contact_number" class="input-short" style="width: 280px;" value="<?php if(isset($user_details[0]['contact_number']) && $user_details[0]['contact_number']!=Null)echo $user_details[0]['contact_number']; ?>" onkeypress="return isNumberKey(event)">
		                    </p>
		                    <p>
		                        <label>Address<span style="color: red;">*</span></label>
		                        <textarea rows="7" cols="90" name="User[address]" id="address" class="input-short"><?php if(isset($user_details[0]['address']) && $user_details[0]['address']!=Null)echo $user_details[0]['address']; ?></textarea>
		                    </p>
		                    <p>
		                        <label>City<span style="color: red;">*</span></label>
		                        <input type="text" name="User[city]" id="city" class="input-short" style="width: 280px;" value="<?php if(isset($user_details[0]['city']) && $user_details[0]['city']!=Null)echo $user_details[0]['city']; ?>">
		                    </p>
		                    <p>
		                        <label>State<span style="color: red;">*</span></label>
		                        <input type="text" name="User[state]" id="state" class="input-short" style="width: 280px;" value="<?php if(isset($user_details[0]['state']) && $user_details[0]['state']!=Null)echo $user_details[0]['state']; ?>">
		                    </p>
                            <p>
		                        <label>Country<span style="color: red;">*</span></label>
		                        <input type="text" name="User[country]" id="country" class="input-short" style="width: 280px;" value="<?php if(isset($user_details[0]['country']) && $user_details[0]['country']!=Null)echo $user_details[0]['country']; ?>">
		                    </p>
		                    <p>
		                        <label>Pin<span style="color: red;">*</span></label>
		                        <input type="text" name="User[pincode]" id="pincode" class="input-short" style="width: 280px;" value="<?php if(isset($user_details[0]['pincode']) && $user_details[0]['pincode']!=Null)echo $user_details[0]['pincode']; ?>" >
		                    </p>
		                    
		                
		             </div> <!-- End .module-body -->
		
		        </div>  <!-- End .module -->
				
		    </div>
		    
		    <div class="grid_12">
		    <fieldset>
		    	&nbsp;
		        <input class="submit-green" type="submit" name="save" value="Submit" /> 
		        <input class="submit-gray" type="reset" value="Cancel" />
		    </fieldset>
		    </div>
		    <div style="clear:both;"></div>
		 </form>    
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>           
       